const models = require("../db/mysqldb/index");
const { Op, where } = require("sequelize");

class DataSystemLogininforModel {
  /**
   * @description create
   * @param {String} login_name,
   * @param {String} ipaddr,
   * @param {String} login_location,
   * @param {String} browser,
   * @param {String} os,
   * @param {String} status,
   * @param {String} msg,
   * @returns
   */
  static async create({
    login_name,
    ipaddr,
    login_location,
    browser,
    os,
    status,
    msg,
  }) {
    return await models.sys_logininfor.create({
      login_name,
      ipaddr,
      login_location,
      browser,
      os,
      status,
      msg,
    });
  }
  /**
   * @description 删除
   * @param {*} op_user_id
   * @param {*} info_id
   * @returns
   */
  static async delete({ op_user_id, info_id }) {
    return await models.sys_post.update(
      {
        del_flag: "2",
        del_by: op_user_id,
      },
      {
        where: { post_id },
      }
    );
  }
  /**
   * @description 批量删除
   * @param {*} op_user_id
   * @param {*} ids
   * @returns
   */
  static async deleteByIds({ op_user_id, ids }) {
    return await models.sys_logininfor.update(
      {
        del_flag: "2",
        del_by: op_user_id,
      },
      {
        where: {
          info_id: {
            [Op.in]: ids,
          },
        },
      }
    );
  }
  /**
   * @description 批量删除
   * @param {*} op_user_id
   * @param {*} ids
   * @returns
   */
   static async clean({ op_user_id }) {
    return await models.sys_logininfor.update(
      {
        del_flag: "2",
        del_by: op_user_id,
      },
      {
        where: {
          del_flag: '0'
        },
      }
    );
  }
  /**
   * @description 获取岗位信息
   * @param {String} login_name
   * @param {String} ipaddr
   * @param {String} status
   * @param {String} limit
   * @param {String} size
   * @returns
   */
  static async list({ limit, size, login_name, ipaddr, status }) {
    let querySql = "";
    if (login_name) {
      querySql += ` and login_name like '%${login_name}%'`;
    }
    if (ipaddr) {
      querySql += ` and ipaddr like '%${ipaddr}%'`;
    }
    if (status) {
      querySql += ` and status = ${status}`;
    }
    let sql = `SELECT
        info_id,
        login_name,
        ipaddr,
        login_location,
        browser,
        os,
        status,
        msg,
        login_time
      FROM
        sys_logininfor WHERE del_flag = '0' ${querySql}
      ORDER BY
      info_id limit ${(limit - 1) * size}, ${size}`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    const count = await models.sequelize.query(
      `SELECT count(1) as count FROM
      sys_logininfor WHERE del_flag = '0' ${querySql};`,
      { replacements: ["active"], type: models.sequelize.QueryTypes.SELECT }
    );
    return {
      result,
      count,
    };
  }
  /**
   * @description 获取岗位信息
   * @param {String} login_name
   * @param {String} ipaddr
   * @param {String} status
   * @returns
   */
  static async findList({ login_name, ipaddr, status }) {
    let querySql = "";
    if (login_name) {
      querySql += ` and login_name like '%${login_name}%'`;
    }
    if (ipaddr) {
      querySql += ` and ipaddr like '%${ipaddr}%'`;
    }
    if (status) {
      querySql += ` and status = ${status}`;
    }
    let sql = `SELECT
        info_id,
        login_name,
        ipaddr,
        login_location,
        browser,
        os,
        status,
        msg,
        login_time
      FROM
        sys_logininfor WHERE del_flag = '0' ${querySql}
      ORDER BY
      info_id`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }

  /**
   * @description 获取所有岗位信息
   * @returns
   */
  static async all({}) {
    let sql = `SELECT
    info_id,
    login_name,
    ipaddr,
    login_location,
    browser,
    os,
    status,
    msg,
        remark
      FROM
        sys_logininfor WHERE del_flag = '0'
      ORDER BY
        create_time DESC,
        update_time DESC`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }
}
module.exports = {
  DataSystemLogininforModel,
};
